package com.aitesting.common;

import com.aitesting.constant.AssertEnum;
import io.restassured.response.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.*;

/**
 * 解析接口返回的数据,提取关联参数
 */
public class ResponseObject {
    private Response response;

    public ResponseObject(Response response) {
        this.response = response;
    }

    /**
     * 提取服务端返回的参数
     * @param extractors 需要解析的路径信息
     * @return Map<String, Object> 提取的参数
     */
    public Map<String, Object> extract(Map<String, String> extractors){
        Map<String, Object> result = new HashMap<>();
        Object field_value = null;

        //HashMap对象类型的遍历
        for (Map.Entry<String, String> entry : extractors.entrySet()) {
            String path = entry.getValue();
            String[] items = path.split("\\.");

            //取出body里面的变量信息
            if (items[0].equals("body")){
                String s = path.replaceFirst(items[0] + "\\.", "");
                field_value = response.jsonPath().getString(s);
            }
            //取出headers里面变量信息
            else if (items[0].equals("headers")) {
                field_value = response.header(items[1]);
            }
            else {
                field_value = response.jsonPath().getString(path);
            };
            result.put(entry.getKey(), field_value);
        }
        return result;
    }

    /**
     * 解析断言信息，进行二次封装
     * @param validators 断言信息 key:断言方式 value:断言数据[json_path, expect_value, message]
     *                   //进行二次封装成Map<String, Object>类型</>
     *                   key:assert  value:断言方式--equal,contains...
     *                   key:path    value:json_path
     *                   key:expect  value:expect_value
     *                   key:message value:message
     */
    public void validate(List<Map<String,Object>> validators, Map<String, Object> variables_mapping){
        for (Map<String, Object> map : validators) {
            // 对断言类型做统一处理  断言信息：格式map: key:eq，value:[json_path,expected_value,message]
            Map<String, Object> validator = new HashMap<>();
            for (String key : map.keySet()) {
                List values = (List) map.get(key);
                validator.put("assert",Utils.get_uniform_comparator(key));
                validator.put("path",values.get(0));
                Object expect_value = Parser.parse_data(values.get(1),variables_mapping);
                validator.put("expect",expect_value);
                this.assertRestAssure(validator);
            }
        }
    }

    /**
     * 执行断言
     * @param validator 封装后的断言信息
     */
    public void assertRestAssure(Map<String, Object> validator){
        for (String key : validator.keySet()) {
            //解析validator中的path和期望值
            AssertEnum assertEnum = (AssertEnum) validator.get("assert");
            String json_path = validator.get("path").toString();
            Object expect_value = validator.get("expect");
//            String message = validator.get("message").toString();
            switch (assertEnum){
                //相等断言
                case equal:
                    response.then().body(json_path,equalTo(expect_value));
                    break;
                //不相等断言
                case notEqual:
                    response.then().body(json_path,not(expect_value));
                    break;
                //包含...字符串断言
                case contains:
                    response.then().body(json_path,containsString(expect_value.toString()));
                    break;
                //hasKey断言
                case hasKey:
                    response.then().body(json_path,hasKey(expect_value));
                    break;
            }
        }
    }
}
